import http from '../../services/http';

const state = {
  userinfo: '',
  usertype: "isp_provider_user",/*isp_provider_admin*/
  token: '',
};


const mutations = {
  updateUser (state, payload) {
    switch (payload.name) {
      case 'userinfo':
        state.userinfo = {...state.userinfo,...payload.userinfo};
        sessionStorage.setItem("userinfo",JSON.stringify(state.userinfo));
        break;
    	case 'token':
	      state.token = payload.token;
	      break;
      default:
        console.log('Error:没有该字段');
    }
  },
  getLocalUser (state) {
  	let userinfo = sessionStorage.getItem('userinfo');
  	if(userinfo)userinfo = JSON.parse(userinfo);
  	console.log(userinfo);
  	state.userinfo = userinfo;
    state.token = sessionStorage.getItem('token');
    state.usertype = userinfo?userinfo.myusertype:"isp_provider_user";
    //state.usertype = "isp_provider_admin";/*管理员登录测试*/
  },
  setUser (state, payload) {
  	state.userinfo = payload.userinfo;
  	state.token = payload.token;
  },
  logout (state) {
    sessionStorage.removeItem('userinfo');
    sessionStorage.removeItem('token');
    state.userinfo = '';
    state.token = '';
  }
};

const actions = {
 	getUserInfo({ commit }, payload) {
		return new Promise((resolve, reject) => {
			http.get("/isp/api/v1/protected/user/queryUser",{
			//http.get("/upm/api/v1/protected/user/me",{
				userNo: payload.userNo
			}, (redata) => {
				if(redata.success) {
					commit({
						type: 'updateUser',
						name: 'userinfo',
						userinfo: redata.data
					});
					resolve(redata);
				} else {
					reject('获取用户信息--失败');
				}
			})
		})
	},
};

export default {
  state,
  mutations,
  actions
};
